package tools

import (
	"math"
)

type Page struct {
	CurPage      int   //当前页
	Pagesize  	int   //每页条数
	Total     	int   //总条数
	PageCount 	int   //总页数
	Nums      	[]int //分页序数
	NumsCount 	int   //总页序数
}

func CreatePaging(curPage, pagesize, total int) Page {
	if curPage < 1 {
		curPage = 1
	}
	if pagesize < 1 {
		pagesize = 12
	}
	page_count := math.Ceil(float64(total) / float64(pagesize))
	page := Page{}
	page.CurPage = curPage
	page.Pagesize = pagesize
	page.Total = total
	page.PageCount = int(page_count)
	page.NumsCount = 7
	page.setNums()
	return page
}

func (this *Page) setNums() {
	this.Nums = []int{}
	if this.PageCount == 0 {
		return
	}

	half := math.Floor(float64(this.NumsCount) / float64(2))
	begin := this.CurPage - int(half)
	if begin < 1 {
		begin = 1
	}

	end := begin + this.NumsCount - 1
	if end >= this.PageCount {
		begin = this.PageCount - this.NumsCount + 1
		if begin < 1 {
			begin = 1
		}
		end = this.PageCount
	}

	for i := begin; i <= end; i++ {
		this.Nums = append(this.Nums, i)
	}
}